using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Home_Insert : System.Web.UI.Page
{
    //Databaseconnect// 
    string _connectionstring = "Server=NontananYe.mssql.somee.com;Uid=NontananYe_SQLLogin_1;PASSWORD=mkjsaf29n8;database=NontananYe;Max Pool Size=400;Connect Timeout=600;";

    protected void Page_Load(object sender, EventArgs e)
    {

        //DataBindRadioBoxList();
        //DataBindChkBoxList();

    }



    private void DataBindChkBoxList()
    {
        try
        {

            string _sql = "SELECT * FROM NontananYe_SQLLogin_1.Color";
            SqlConnection _connection = new SqlConnection(_connectionstring);
            SqlCommand _command = new SqlCommand(_sql, _connection);
            SqlDataAdapter _adapter = new SqlDataAdapter(_command);
            DataTable _table = new DataTable();
            _adapter.Fill(_table);

            myCBoxList1.DataSource = _table;
            myCBoxList1.DataTextField = "C_Name";
            myCBoxList1.DataValueField = "C_ID";
            myCBoxList1.DataBind();
        }
        catch
        {
            throw;
        }

    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        string SQL = "insert into  NontananYe_SQLLogin_1.Customer (Cus_FirstName,Cus_Lastname,Cus_NickName,Cus_FavoritesFood) Values (@name,@lastname,@nick,@food)";
        SqlConnection con = new SqlConnection(_connectionstring);
        SqlCommand cmd = new SqlCommand(SQL, con);

        String a = Convert.ToString(RadioButtonList1.SelectedItem.Value);
        double AA = RadioButtonList1.SelectedIndex + 1;


        cmd.Parameters.AddWithValue("@name", textboxx1.Text);
        cmd.Parameters.AddWithValue("@lastname", textboxx2.Text);
        cmd.Parameters.AddWithValue("@nick", textboxx3.Text);
        cmd.Parameters.AddWithValue("@food", AA);


        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            insertCBL();
           
            textboxx1.Text = String.Empty;
            textboxx2.Text = String.Empty;
            textboxx3.Text = String.Empty;


            RadioButtonList1.ClearSelection();
            myCBoxList1.ClearSelection();
        }
        catch (Exception ex)
        {
            textboxx1.Text = ex.Message;
        }
        finally
        {

            Response.Write("<script> language='javascript'>alert('record has been added');</script>");
            con.Close();
        }

    }

    protected void Button2_Click(object sender, EventArgs e)
    {
       
       
       

    }

    private void insertCBL()
    {
        int i;

        for (i = 0; i < myCBoxList1.Items.Count; i++)
        {

            if (myCBoxList1.Items[i].Selected)
            {
                string SQL = "insert into  NontananYe_SQLLogin_1.Customer_Detail (CusDetail_Cus_ID,CusDetail_Color_ID) Values ( (Select Top 1 Cus_ID from  NontananYe_SQLLogin_1.Customer ORDER BY Cus_ID DESC ),@color)";
                SqlConnection con = new SqlConnection(_connectionstring);
                SqlCommand cmd = new SqlCommand(SQL, con);

                
                double AA = RadioButtonList1.SelectedIndex + 1;

                ;
                cmd.Parameters.AddWithValue("@color", myCBoxList1.Items[i].Value);
                
               
                myCBoxList1.Items[i].Selected = false;
                insertCBL();

                try
                {
                    

                    textboxx1.Text = String.Empty;
                    textboxx2.Text = String.Empty;
                    textboxx3.Text = String.Empty;


                    RadioButtonList1.ClearSelection();
                    myCBoxList1.ClearSelection();
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();


                }
                catch (Exception ex)
                {
                    textboxx1.Text = ex.Message;
                }
                finally
                {

                 
                }
                //string BB += myCBoxList1.Items[i].Text.ToString() + ",";
               

            }
        }
    }

   

 


    private void DataBindRadioBoxList()
    {

        try
        {

            string _sql = "SELECT F_ID,F_Name FROM NontananYe_SQLLogin_1.Food";
            SqlConnection _connection = new SqlConnection(_connectionstring);
            SqlCommand _command = new SqlCommand(_sql, _connection);
            SqlDataAdapter _adapter = new SqlDataAdapter(_command);
            DataTable _table = new DataTable();
            _adapter.Fill(_table);

            RadioButtonList1.DataTextField = "F_Name";
            RadioButtonList1.DataValueField = "F_ID";
            RadioButtonList1.DataSource = _table;
            RadioButtonList1.DataBind();
        }
        catch
        {
            throw;
        }
    }
    protected void Button100_Click(object sender, EventArgs e)
    {
        

    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        Button4.Visible = false;
        DataBindRadioBoxList();
        DataBindChkBoxList();
        MyTable.Visible = true;

    }
    

    private void Check_box()
    {
        //Double YY = myCBoxList1.TextChanged;
        int i;
        
        for (i = 0; i < myCBoxList1.Items.Count ; i++)
        {

            if (myCBoxList1.Items[i].Selected)
            {
             //string BB += myCBoxList1.Items[i].Text.ToString() + ",";
                string cc = myCBoxList1.Items[i].Value.ToString();
            }
               
        }

      // Label8.Text = AA;
    }
    
}